const app = getApp();
const req = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    page: 1,
    size: 15,
    total: '',
    newList: [], //列表
    statistics: [],
    showModalStatus: false,
    headerTime: '',
    paymentList: [
      {
        id: 0,
        value: '全部'
      }, {
        id: 1,
        value: '微信'
      }, {
        id: 2,
        value: '支付宝'
      }, {
        id: 3,
        value: '会员支付'
      }, {
        id: 4,
        value: '通用通'
      }],
    typeId: '0',
    orderType: '0', //支付类型
    fields: 'day',
    onShowHidd:false,
    payType: '',
    renovate: 'false',
    index: 0,
    showTime: false,
    storeId: '', //门店id
    billId: '',//账单ID
    isPickerRender: false, //时间弹框
    isPickerShow: false,
    array: [],
    // statusList: [{ id: 0, name: '全部' },{ id: 1, name: '已支付'}, { id: 3, name: '已退款' }, { id: 5, name: '押金支付' }],
    params: {},
    current: '0',
    date: '',//开始时间
    endDate: '',//结束时间
    itemDate: '',
    orderStatus: '',
    billList: [{ id: 0, name: '日账单' }, { id: 1, name: '月账单' }, { id: 2, name: '年账单' }], //账单类型
    isShowTime: true,// 时间类型
  },

  
  

  // ----详情列表------
  transaction(page, size, params) {
    let t = this
    req.wxReq(`/api/statistics/getClerkOrderList/${page}/${size}`, "GET", params).then(res => {
      let {
        // list,
        statistics
      } = t.data
      let result = res.data.result
      if (res.data.code == 200) {
        let list = result.page.records
        for (let i = 0; i < list.length; i++) {
          let item = list[i]
          if (item.orderStatus == '0') {
            item.orderStatus = '未支付'
            item.typeColor ='#b67b1a'
          } else if (item.orderStatus == '1') {
            item.orderStatus = '已完成'
            item.typeColor = '#3EBA79'
          } else if (item.orderStatus == '2') {
            item.orderStatus = '支付失败'
            item.typeColor = '#ac192b'
          } else if (item.orderStatus == '3') {
            item.orderStatus = '已退款'
            item.typeColor = '#ac192b'
          } else if (item.orderStatus == '4') {
            item.orderStatus = '已撤销'
            item.typeColor = '#885427'
          } else if (item.orderStatus == '5') {
            item.orderStatus = '已消费'
            item.typeColor = '#f55d23'
          }

          if (item.payType == '1') {
            item.payType = '微信'
          } else if (item.payType == '2') {
            item.payType = '支付宝'
          } else if (item.payType == '3') {
            item.payType = '会员支付'
          } else if (item.payType == '4') {
            item.payType = '通用通'
          }

          if (item.tradeType == 'FACEPAY') {
            item.tradeType = '人脸'
          } else if (item.tradeType == 'SCAN') {
            item.tradeType = '主扫'
          } else if (item.tradeType == 'SWEPT') {
            item.tradeType = '被扫'
          } 

        }
        let newList = t.data.newList.concat(list)
        t.setData({
          newList,
          statistics: result.statistics,
          total: result.page.total,
          showModalStatus: false
        })
     
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 500
        })
      }
    })
  },

  // --点击查询---
  filtrateClik(e) {
    let t = this
    let page = 1
    let {
      size,
      storeUserId
    } = t.data
    let params = e.detail.params
    params.storeUserId = storeUserId
    t.setData({
      params,
      newList: []
    })
    t.transaction(page, size, params)

  },

  // --跳转详情页---
  detailsClick(e) {
    let details = JSON.stringify(e.currentTarget.dataset.details)
    let username = this.data.username
    let outTradeNo = e.currentTarget.dataset.outtradeno
    let params = JSON.stringify(this.data.params)
    wx.navigateTo({
      url: '../transaction_03/transaction_03?details=' + details + '&outTradeNo=' + outTradeNo + '&params=' + params,
    })
  },

  // -----弹框层----------
  clickme() {
    this.showModal();
  },
  //显示对话框
  showModal() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    //在 Y 轴平移的距离
    animation.translateY(300).step()
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },

  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  //退出登录
  exitLogin() {
    wx.showModal({
      title: '提示',
      content: '是否切换账号？',
      success(res) {
        if (res.confirm) {
          wx.redirectTo({
            url: '../index/index'
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let t = this
    let storeUserId = Number(options.storeUserId);
    var windowHeight = wx.getSystemInfoSync().windowHeight
    let {
      page,
      size,
    } = t.data
    let params =''
    if (options.params){
       params = JSON.parse(options.params.replace(/storeId/g, "storeUserId"))
      params.storeUserId = storeUserId
    }
  
    // t.transaction(page, size, params)
    wx.setNavigationBarTitle({
      title: app.globalData.name
    })
    let myData = new Date();
    let getFullYear = myData.getFullYear();
    let getMonth = myData.getMonth() + 1;
    let getDate = myData.getDate();
    if (getDate < 10) {
      getDate = '0' + getDate
    }
    if (getMonth < 10) {
      getMonth = '0' + getMonth
    }
    let currentDate = getFullYear + '-' + getMonth + '-' + getDate; //年月日
    let currentMonth = getFullYear + '-' + getMonth  //年月
    let currentYear = getFullYear //年
    t.setData({
      date: currentDate,
      endDate: currentDate,
      username: wx.getStorageSync("username"),
      type: options.type,
      itemDate: params.queryTime,
      currentMonth,
      currentYear,
      currentDate,
      storeUserId,
      params,
      newList:[],
      scrollHight: windowHeight - 219
    })
  },
  

  onShow:function (e){ 
    let { params,size} = this.data
    let page=1
    this.setData({
      newList:[]
    })
    if (this.data.renovate=='true'){//详情页跳转过来
      this.transaction(page, size, params)
    }else{
      let {params} = this.data
      this.transaction(page, size, params)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    let t = this
    let page = 1
    let {
      // page,
      size,
      params
    } = t.data
    if (this.data.params) {
      t.setData({
        newList: []
      })
    }

    t.transaction(page, size, params)
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let t = this
    var page = t.data.page + 1;
    t.setData({
      page,
    })
    if (page > Math.ceil(this.data.total / this.data.size)) {
      wx.showToast({
        title: '到底了！！！',
        icon: 'none'
      })
      return;
    };
    var page = t.data.page;
    let params = t.data.params
    this.transaction(page, this.data.size, params)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})